<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Server Side Scripts &mdash; Documentation</title>
    <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="Documentation" href="index.html" />
    <link rel="up" title="Writing Scripts" href="writing_scripts.html" />
    <link rel="next" title="Creating Pages" href="creating_pages.html" />
    <link rel="prev" title="Writing Scripts" href="writing_scripts.html" /> 
  </head>
  <body>
    <div style="background-color: #FFF; text-align: left; padding: 8px 0px"><img src="_static/banner300910.gif"></div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="creating_pages.html" title="Creating Pages"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="writing_scripts.html" title="Writing Scripts"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">Documentation</a> &raquo;</li>
          <li><a href="build_app.html" >2. Building an Application</a> &raquo;</li>
          <li><a href="writing_scripts.html" accesskey="U">Writing Scripts</a> &raquo;</li> 
      </ul>
    </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="index.html">Table Of Contents</a></h3>
            <ul>
<li><a class="reference external" href="">Server Side Scripts</a><ul>
<li><a class="reference external" href="#introduction">Introduction</a></li>
<li><a class="reference external" href="#declaring-a-class">Declaring a Class</a></li>
<li><a class="reference external" href="#validate-method">validate method</a></li>
<li><a class="reference external" href="#on-update-on-submit-on-cancel-methods">on_update, on_submit, on_cancel methods</a></li>
<li><a class="reference external" href="#adding-child-records">Adding Child Records</a></li>
<li><a class="reference external" href="#debugging">Debugging</a></li>
</ul>
</li>
</ul>

            <h4>Previous topic</h4>
            <p class="topless"><a href="writing_scripts.html"
                                  title="previous chapter">Writing Scripts</a></p>
            <h4>Next topic</h4>
            <p class="topless"><a href="creating_pages.html"
                                  title="next chapter">Creating Pages</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/server_side_script.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="server-side-scripts">
<h1>Server Side Scripts<a class="headerlink" href="#server-side-scripts" title="Permalink to this headline">¶</a></h1>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>On the server-side, scripts are embedded in DocTypes. All scripts have to reside in classes in the DocTypes.</p>
<p>To add a server script, open a DocType you want to attach the script to and open the &#8220;Server Script&#8221; tab.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you do not want the server script to be attached to any particular DocType, or call it from many
DocTypes, you can create a &#8220;Single&#8221; DocType. Then using the get_obj method, you can call it from
anywhere. More about it later.</p>
</div>
</div>
<div class="section" id="declaring-a-class">
<h2>Declaring a Class<a class="headerlink" href="#declaring-a-class" title="Permalink to this headline">¶</a></h2>
<p>Server Side methods (functions) always reside in a &#8220;DocType&#8221; class, hence all your DocType classes will
be declared in the following manner:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">DocType</span><span class="p">:</span>

   <span class="c"># standard constructor</span>
   <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">doclist</span><span class="p">):</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">doc</span> <span class="o">=</span> <span class="n">doc</span>
      <span class="bp">self</span><span class="o">.</span><span class="n">doclist</span> <span class="o">=</span> <span class="n">doclist</span>
</pre></div>
</div>
<p>Let us see this constructor line by line</p>
<ol class="arabic simple">
<li><strong>class DocType</strong> - This is the standard declaration of a class (for any DocType, the class will be labeled DocType)</li>
<li><strong>def __init__(self, doc, doclist):</strong> - This is the constructor. The object will be constructed by the framework
and the framework will supply the data record &#8220;doc&#8221; and a bundle of data-records including child records
of this object in &#8220;doclist&#8221;</li>
<li><strong>self.doc = doc</strong> - Set class property &#8220;doc&#8221; as the data object</li>
<li><strong>self.doclist = doclist</strong> - Set the class property &#8220;doclist&#8221; as the list of child records</li>
</ol>
</div>
<div class="section" id="validate-method">
<h2>validate method<a class="headerlink" href="#validate-method" title="Permalink to this headline">¶</a></h2>
<p>The validate method is called just before the user saves a record using the &#8220;Save&#8221; button. To stop the user
from saving, raise an Exception</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">validate</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">doc</span><span class="o">.</span><span class="n">start_date</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">doc</span><span class="o">.</span><span class="n">finish_date</span><span class="p">:</span>
       <span class="n">msgprint</span><span class="p">(</span><span class="s">&#39;Start date must be before finish date&#39;</span><span class="p">)</span>
       <span class="k">raise</span> <span class="ne">Exception</span>
</pre></div>
</div>
</div>
<div class="section" id="on-update-on-submit-on-cancel-methods">
<h2>on_update, on_submit, on_cancel methods<a class="headerlink" href="#on-update-on-submit-on-cancel-methods" title="Permalink to this headline">¶</a></h2>
<p>These methods are called at various stages of saving a document, as defined in <a class="reference external" href="save_submit.html"><em>Save-Submit-Cancel Pattern</em></a></p>
<p>The on_update method is called after the document values are saved in the database. If you raise an
Exception in any of these methods, the entire transaction will be rolled back.</p>
</div>
<div class="section" id="adding-child-records">
<h2>Adding Child Records<a class="headerlink" href="#adding-child-records" title="Permalink to this headline">¶</a></h2>
<p>Child records can be added on the server side by the addchild method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">addchild</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">fieldname</span><span class="p">,</span> <span class="n">childtype</span> <span class="o">=</span> <span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">local</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">doclist</span><span class="o">=</span><span class="bp">None</span><span class="p">)</span>
</pre></div>
</div>
<p>here is an example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">c</span> <span class="o">=</span> <span class="n">Document</span><span class="p">(</span><span class="s">&#39;Contact&#39;</span><span class="p">,</span><span class="s">&#39;ABC&#39;</span><span class="p">)</span>
<span class="n">d</span> <span class="o">=</span> <span class="n">addchild</span><span class="p">(</span><span class="n">c</span><span class="p">,</span> <span class="s">&#39;contact_updates&#39;</span><span class="p">,</span> <span class="s">&#39;Contact Update&#39;</span><span class="p">,</span> <span class="n">local</span> <span class="o">=</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">d</span><span class="o">.</span><span class="n">last_updated</span> <span class="o">=</span> <span class="s">&#39;Phone call&#39;</span>
<span class="n">d</span><span class="o">.</span><span class="n">save</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="debugging">
<h2>Debugging<a class="headerlink" href="#debugging" title="Permalink to this headline">¶</a></h2>
<p>For de-bugging on the server side, you can</p>
<ol class="arabic simple">
<li>Print messages via msgprint(message)</li>
<li>Print error messages via errprint(message)</li>
</ol>
<p>The full traceback of your error can be seen in <strong>Tools -&gt; Error Console</strong></p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="creating_pages.html" title="Creating Pages"
             >next</a> |</li>
        <li class="right" >
          <a href="writing_scripts.html" title="Writing Scripts"
             >previous</a> |</li>
        <li><a href="index.html">Documentation</a> &raquo;</li>
          <li><a href="build_app.html" >2. Building an Application</a> &raquo;</li>
          <li><a href="writing_scripts.html" >Writing Scripts</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2010, Rushabh Mehta.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.3.
    </div>
  </body>
</html>